package com.come4loves.game.dataconfig.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.come4loves.core.dataconfig.ServiceAdapter;
import com.come4loves.core.utils.Splitable;
import com.come4loves.game.dataconfig.model.UnitCardConfig;

/**
 * 单位卡
 * 
 * @author jerry
 *
 */
@Component
public class UnitCardService extends ServiceAdapter {
	
	protected static Logger LOGGER = LoggerFactory.getLogger(UnitCardService.class);

	/**
	 * 物品配置
	 */
	private static Map<Integer, UnitCardConfig> unitCardsMap = new HashMap<Integer, UnitCardConfig>();

	@Override
	public void clear() {
		unitCardsMap.clear();
	}

	@Override
	public void initialize() {
		List<UnitCardConfig> list = this.dataConfig.listAll(this, UnitCardConfig.class);
		for (UnitCardConfig config : list) {
			unitCardsMap.put(config.getId(), config);
		}
	}

	public static UnitCardConfig get(int id) {
		if (!unitCardsMap.containsKey(id)){
			LOGGER.error("unit card not exsit, id:{}", id);
		}
		return unitCardsMap.get(id);
	}

	public static boolean exsit(int goodsId) {
		return unitCardsMap.containsKey(goodsId);
	}
	
	
	public static String exportAllUnit() {
		StringBuffer sBuffer = new StringBuffer();
		for (UnitCardConfig cardConfig : unitCardsMap.values()) {
			sBuffer.append(cardConfig.toString()).append(Splitable.ELEMENT_DELIMITER);
		}
		return sBuffer.deleteCharAt(sBuffer.length() - 1).toString();
	}
}
